
%macro prnt2(program=, disname=, ds_list=, vis=, popfl=,tblno=);

%get_data(lst=adqs adsl,pre=,lib=adamw,supp=y);

data qs1;
	set adqs;
	where pdfl='Y' and %if &tblno=2 %then %do; upcase(parcat1)='TOBACCO/NICOTINE WITHDRAWAL' and index(paramcd,'EMAX')  %end; 
	%else %if &tblno=3 %then %do; upcase(parcat1)='DIRECT EFFECTS OF PRODUCT' and index(paramcd,'EMAX') %end;;
	if trta='A' then do;
		trtpn=trtan;
		trtp=trta;
		column=1;
	end;
	if trta='B' then do;
		trtpn=trtan;
		trtp=trta;
		column=2;
	end;
	if trta='C' then do;
		trtpn=trtan;
		trtp=trta;
		column=3;
	end;
	if trta ne '' then output;
	trtpn=4;
	trtp='Overall';
	column=4;
	output;
run;

data pop;
	set adsl;
	where pdfl='Y';
	array trts trt01pn trt02pn trt03pn trt04pn trt05pn trt06pn;
	do over trts;
		if trts=1 then do;
			column=1;
			trtpn=1;
			trtp='Product A';
			output;
		end;
		if trts=2 then do;
			column=2;
			trtpn=2;
			trtp='Product B';
			output;
		end;
		if trts=3 then do;
			column=3;
			trtpn=3;
			trtp='Product C';
			output;
		end;
	end;
	column=4;
	trtpn=4;
	trtp='Overall';
	output;
run;

data pop;
	set pop;
	tocount=1;
run;

Proc sort data =pop out = spopone nodupkey ;
   by column trtp;
run;
Proc Print data = spopone noobs n width = min;
var column trtp;
run;
data tofmt;
   set spopone;
run;

%makefmt(tofmt, column , trtp, _ftrt);
*******************************************************************************************************
Define population for each column
******************************************************************************************************;
%popu(datain=pop);
%macro a;
%do i = 1 %to &maxtrt;
   %put pop&i=&&&pop&i;
%end;
%mend a; %a;

data _heads;
  format n1-n4 $15. lbl1-lbl4 $150.;

  n1 = "(N=%cmpres(&pop1))";
  n2 = "(N=%cmpres(&pop2))";
  n3 = "(N=%cmpres(&pop3))";
  n4 = "(N=%cmpres(&pop4))";
  *n5 = "(N=%cmpres(&pop5))";

  lbl1 = strip(put(1, _ftrt.));
  lbl2 = strip(put(2, _ftrt.));
  lbl3 = strip(put(3, _ftrt.));
  lbl4 = strip(put(4, _ftrt.));
  *lbl5 = strip(put(5, _ftrt.));

  call symput("N1",n1);
  call symput("N2",n2);
  call symput("N3",n3);
  call symput("N4",n4);
  *call symput("N5",n5);

  call symput("lbl1",lbl1);
  call symput("lbl2",lbl2);
  call symput("lbl3",lbl3);
  call symput("lbl4",lbl4);
  *call symput("lbl5",lbl5);
run;

proc sql noprint;
create table headers
(column num,
  header char(200));
insert into headers
   %do i = 1 %to 4;
      values(&i, "&&lbl&i.~%str(&&N&i)")
   %end;
;

select header
into :header1-:header99
from headers
where column LE 4
order by column;
quit;

*******************************************************************************************************
Create columns macros
******************************************************************************************************;
data _null_;
   call symput("nbr_report_column", "4");
   call symput("column1", "COL1");
   call symput("column2", "COL2");
   call symput("column3", "COL3");
   call symput("column4", "COL4");
   
run;



*******************************************************************************************************
Define specific formats for the report
******************************************************************************************************;
proc format;
 value level1l  1 = "Value"
                2 = "Change From Baseline"
;
run;

*******************************************************************************************************
Data manipulation
******************************************************************************************************;
proc sort data=qs1;
/*	by param avisitn avisit parcat2 atptn atpt column;*/
	by param paramcd avisitn avisit parcat2 column;
run;

%univariate6c(var=aval, width=5,decimal=0,out=_tmp1,level1=1,level1FMT=level1l,level2=,level2FMT=stat4_,level3=,wh=%str(),
			by= param paramcd avisitn avisit parcat2, in=qs1);

			proc sort data=qs1;
/*	by param avisitn avisit parcat2 atptn atpt column;*/
	by param paramcd parcat2 column;
run;

%univariate6c(var=aval, width=5,decimal=0,out=_tmp2,level1=1,level1FMT=level1l,level2=,level2FMT=stat4_,level3=,wh=%str(),
			by= param paramcd parcat2, in=qs1);


*******************************************************************************************************
Create report dataset
******************************************************************************************************;
data final;
	set _tmp1 _tmp2; 
/*	by  param paramcd avisitn avisit parcat2 level1 level2;*/
/*	set _tmp1 _tmp2 ; */
/*	by  param avisitn avisit parcat2 atptn atpt level1 level2;*/
	if LEVEL2_LABEL="N" then LEVEL2_LABEL="n";
	if avisitn=. then do;
		avisitn=99;
		avisit='Overall';
	end;
run;

data final;
	set final;
	length level1c $100.;
	level1c = put(level1,level1l.);
	if level2>2 then level2=level2+1;
	if level2=7 then level2=3;
run;


proc sort data=final out=report;
/*	by param avisitn avisit parcat2 atptn atpt level1 level2;*/
	by param paramcd avisitn avisit parcat2 level1 level2;
run;


/*%page_cut(in=report,sort=param avisitn avisit parcat2 level1 level1c level2 level2_label col1 col2 col3 col4,*/
/*nocut=atpt,pagesize=32,skip=level1c,*/
/*columns=param avisit parcat2 level1c level2_label col1 col2 col3 col4,*/
/*width=  1 0.8 1 1 1 1 1 1 1 1,out=repport);*/

/*%page_cut(in=report,sort=param avisitn avisit parcat2 level1 level1c level2 level2_label col1 col2 col3,*/
%page_cut(in=report,sort=param avisitn avisit parcat2 level1 level1c level2 level2_label ,
nocut=avisit,pagesize=35,skip=level1c ,
columns=param avisit parcat2 level1c level2_label col1 col2 col3 col4,
width=  2.8    1.0      0.9      1.0        1.0   1.0  1.0  1.0  0.9 ,   out=repport);

proc sort data=repport out=repport;
by page param avisitn avisit parcat2 level1 ;
run;

*Temporary!!!!!!;
/*data repport;*/
/*set repport;*/
/*by page param avisitn avisit parcat2 level1 ;*/
/*if param=" " then do;*/
/*avisitn=.; avisit=" "; parcat2=" "; level1=.; level1c=" "; level2=.; level2_label=" "; level1_label=" ";*/
/*end;*/
/*if first.param;*/
/*run;*/

proc sql;
 select count(distinct param)
 into :nb_obse separated by ' ' 
 from repport where param ne " ";
quit;

/*data repport;*/
/*set repport;*/
/*by param paramcd avisitn avisit parcat2 level1 ;*/
/* retain nb page 0;*/
/* if (first.param and nb GE 24) then do;*/
/*  page+1;*/
/*  nb=1;*/
/* end;*/
/* else nb+1;*/
/*run;*/

*******************************************************************************************************
Printing using proc report
******************************************************************************************************;
title3 "Table &disname (Continued)";
%footer1(foot1=1, ds_list=&ds_list.,program=&program.);

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&disname..QS.SUM.SUBJ.rtf";

%open_rtf;
%empty_ds_fix(report);

%if &tblno=2 %then %do;
title4 "Summary of Subjective Effect Parameters for Tobacco/Nicotine Withdrawal" ;
%end;

%if &tblno=3 %then %do;
title4 "Summary of Subjective Effect Parameters for Direct Effect of Product Questionnaire" ;
%end;

title5 "(Pharmacodynamic Population)";

/*footnote3 j=l "Note(s): N is by parameter either per subjects or per units.";*/
/*footnote4 j=l "         Estimated nicotine uptake = total cartridge weight change (mg) x nicotine concentration of the liquid (% by weight)";*/
/*footnote5 j=l " ";*/
/*footnote6 j=l " ";*/
/*footnote7 j=l " ";*/


proc report data=repport nowd split='~' missing;
/* column page param avisitn avisit parcat2 atptn atpt level1 level1c level2 level2_label col1 col2 col3 col4;*/

 column page param avisitn avisit parcat2 level1 level1c level2 level2_label %do j=1 %to 3; col&j  %end;; /*col4*/;
 define page         / " " order order=internal noprint;
 define level1       / " " order order=internal noprint;
 define level2       / " " order order=internal noprint;
 define avisitn       / "" order order=internal noprint;
/* define atptn       / "" order order=internal noprint;*/
 define Param / "Subjective Effect~Parameter" order group order=internal style(column)=[cellwidth=&__param.in just=left] style(header)=[just=left];
 define avisit / "Visit" order group order=internal style(column)=[cellwidth=&__avisit.in just=left] style(header)=[just=left];
 define parcat2 / "Product Use~Session" order group order=internal style(column)=[cellwidth=&__parcat2.in just=left] style(header)=[just=left];
/* define atpt / "Time Point" order group order=internal style(column)=[cellwidth=&__atpt.in just=left] style(header)=[just=left];*/
 define level1c / " " order group order=internal style(column)=[cellwidth=&__level1c.in just=left] style(header)=[just=left];
 
 define level2_label / " " order order=internal style(column)=[cellwidth=&__level2_label.in just=left] style(header)=[just=left];

 %do j = 1 %to 3; 
 	define col&j / "&&header&j." style(column)=[cellwidth=1in just=center] style(header)=[just=center];
 %end;

 
 break after page   / page;
 %if &nb_obse GT 0 %then %do;

compute before level1c / style=[cellwidth=6in just=left];
  line " ";
endcomp;
/**/
/*compute after param / style=[cellwidth=6in just=left];*/
/*  line " ";*/
/*endcomp;*/

 %end;

 %if &nb_obse EQ 0 %then %do;
   compute after page/style=[cellwidth=9.7in just=left];
    line "No Values Were Measured.";
   endcomp;
 %end;

run;

ods rtf close;
ods listing;
%arrange_rtf;
%mend prnt2;

/*%prnt(program=T14_01_04_02_PROD_USE, disname=14.1.4.2, ds_list=ADEX, vis=%str((4 5 6), popfl=%str(SAFFL="Y"));*/
%prnt2(program=T14_02_03_02_0X_QS, disname=14.2.3.2.1, ds_list=ADQS ADSL,tblno=2);
%clrw;

%prnt2(program=T14_02_03_02_0X_QS, disname=14.2.3.2.2, ds_list=ADQS ADSL,tblno=3);
%clrw;
